package com.weaver.qfengx;

import com.google.common.collect.Maps;
import org.junit.Test;
import weaver.general.GCONST;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Function;

/**
 * Created by YeShengtao on 2020/7/30 16:50
 */
public class PropUtils {


    private static final LogUtils log = new LogUtils(PropUtils.class);
    private static String PROP_ROOT_PATH = GCONST.getPropertyPath();

    public static File getPropFile(String filename) {
        File file = new File(PROP_ROOT_PATH + filename);
        return file;
    }

    public static File[] getPropFilelist() {
        String propRootPath = GCONST.getPropertyPath();
        return new File(propRootPath).listFiles((file) -> file.getName().length() > 11 && StringUtils.equals(StringUtils.tailSubstring(file.getName(), 11), ".properties"));
    }

    /**
     * 返回properties文件中指定的参数值
     *
     * @param filename filename
     * @param key      key
     * @return
     */
    public static String get(String filename, String key) {
        try {
            Properties properties = new Properties();
            File file = new File(PROP_ROOT_PATH + filename + ".properties");
            properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)));
            return properties.getProperty(key);
        } catch (Exception e) {
//            e.printStackTrace();
            log.writeLog(filename + ".properties key: " + key + ": " + e.getMessage());
            return "";
        }
    }

    public static String get(String filename, String key, String secondVal) {
        String val = get(filename, key);
        return Objects.isNull(val) ? secondVal : val;
    }

    /**
     * 返回properties文件中符合匹配规则的的参数值集合
     *
     * @param filename filename
     * @param regex    regex
     * @return Map<String, String>
     */
    public static Map<String, String> regexMap(String filename, String regex) {
        Map<String, String> map = Maps.newHashMap();
        try {
            Properties properties = new Properties();
            File file = new File(PROP_ROOT_PATH + filename + ".properties");
            properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)));
            properties.stringPropertyNames().forEach(x -> {
                if (x.matches(regex)) {
                    map.put(x, properties.getProperty(x));
                }
            });
            return map;
        } catch (Exception e) {
            log.writeLog(filename + ".properties regex: " + regex + ": " + e.getMessage());
//            e.printStackTrace();
            return map;
        }
    }

    /**
     * 返回properties文件中符合匹配规则的的参数值集合
     *
     * @param filename filename
     * @param regex    regex
     * @param callable callable
     * @return Map<String, String>
     */
    public static Map<String, String> regexMap(String filename, String regex, Function<String, String> callable) {
        Map<String, String> map = Maps.newHashMap();
        try {
            Properties properties = new Properties();
            File file = new File(PROP_ROOT_PATH + filename + ".properties");
            if (file.exists()) {
                properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)));
            } else {
                properties.load(ClassLoader.getSystemResourceAsStream("HitachiDidi.properties"));
            }
            properties.stringPropertyNames().forEach(x -> {
                if (x.matches(regex)) {
                    map.put(callable.apply(x), properties.getProperty(x));
                }
            });
            return map;
        } catch (Exception e) {
            log.writeLog(filename + ".properties regexMap: " + regex + ": " + e.getMessage());
//            e.printStackTrace();
            return map;
        }
    }

    @Test
    public void test01() {

    }
}
